<?php
declare(strict_types=1);

namespace App\Service;

use App\Constants\MchAccountRemarkMapping;
use App\Constants\StrStatus;
use App\Model\Dao\MchAccountAccessDetailDao;
use App\Model\Enum\EventOrderAccountTypeEnum;
use App\Model\Enum\MchAccountAccessTypeEnum;
use App\Model\Enum\MchAccountInOutTypeEnum;
use App\Utils\CustomUtils;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\MchAccountAccessDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;


class MchAccountAccessService
{

    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var MchAccountAccessDao
     */
    private $MchAccountAccessDao;

    /**
     * @Inject()
     * @var MchAccountAccessDetailDao
     */
    private $MchAccountAccessDetailDao;

    public function get($param)
    {
        $result = $this->MchAccountAccessDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        
        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($result['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        if($param['source'] == 1){            
            //从商户后台过来的请求
            if($result['mchId'] != $param['mchId']){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }  
        } 

        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getMsecToMescdate($result['createTime']);
        $result['strAccessType'] = MchAccountAccessTypeEnum::getLabel($result['accessType']);
        $this->setSerialNo($result);
        $this->strAccountType($param['source'], $result['accountType'], $result);

        $locale = $this->translator->getLocale();
        if ($locale == 'en') {
            $result['remark'] = MchAccountRemarkMapping::REMARK[$result['remark']];
        }

        $detail = $this->MchAccountAccessDetailDao->getWhereList(['accessId' => $param['id']]);
        foreach ($detail as &$value) {
            $value['strInOutType'] = MchAccountInOutTypeEnum::getLabel($value['inOutType']);
            $this->strAccountType($param['source'], $value['accountType'], $value);
            $this->setSerialNo($value);
        }
        $result['detail'] = $detail;

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->MchAccountAccessDao->getCount($param);
        $list = $this->MchAccountAccessDao->getList($param, $page, $limit);

        $locale = $this->translator->getLocale();
        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getMsecToMescdate($v['createTime']);
            $v['strAccessType'] = MchAccountAccessTypeEnum::getLabel($v['accessType']);
            $this->strAccountType($param['source'], $v['accountType'], $v);
            $this->setSerialNo($v);

            if ($locale == 'en') {
                $v['remark'] = MchAccountRemarkMapping::REMARK[$v['remark']];
            }

            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function strAccountType(&$source, &$accountType, &$v) {
        if ($source == 1) {
            // 给商户看的
            $v['strAccountType'] = $accountType == 1 ? '可用账户' : '冻结账户' ;
        } else {
            // 给平台看的
            $v['strAccountType'] = EventOrderAccountTypeEnum::getLabel($v['accountType']);
        }
    }

    public function setSerialNo(&$v) {
        $v['serialNo'] = '';
        if (!empty($v['tradeNo'])) {
            $v['serialNo'] = $v['tradeNo'];
        } else if (!empty($v['eventOrderNo'])) {
            $v['serialNo'] = $v['eventOrderNo'];
        } else if (!empty($v['transNo'])) {
            $v['serialNo'] = $v['transNo'];
        } else if (!empty($v['withdrawNo'])) {
            $v['serialNo'] = $v['withdrawNo'];
        } else if (!empty($v['commissionTransNo'])) {
            $v['serialNo'] = $v['commissionTransNo'];
        }
    }

}